package com.example.app.ui.screens


import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.app.R
import com.example.app.compositionLocal.LocalUserViewModel
import kotlinx.coroutines.launch

@Composable
fun LoginScreen(onClose: () -> Unit) {
//    //屏幕宽度
//    var screenWidth: Float
//    var screenHeight: Float
//    with(LocalDensity.current) {
//        screenWidth = LocalConfiguration.current.screenWidthDp.dp.toPx()
//        screenHeight = LocalConfiguration.current.screenHeightDp.dp.toPx()
//    }
    val userViewModel = LocalUserViewModel.current
    val coroutineScope = rememberCoroutineScope()

    var showPassword by remember {
        mutableStateOf(false)
    }
    BoxWithConstraints(contentAlignment = Alignment.Center, modifier = Modifier.fillMaxSize()) {
        val screenWidth = constraints.maxWidth.toFloat()
        val screenHeight = constraints.maxHeight.toFloat()

        //背景图层
        Image(
            painter = painterResource(id = R.drawable.login_bg),
            contentDescription = null,
            alignment = Alignment.Center,
            modifier = Modifier
                .fillMaxSize()
//                .offset(x=(-50).dp)
                .clip(RectangleShape), // 裁剪超出的部分
            contentScale = ContentScale.Crop,

            )
//        右上往左下渐变层
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(
                    brush = Brush.linearGradient(
                        listOf(Color(0xffbb8378), Color.Transparent),
                        start = Offset(x = screenWidth, y = 0f),
                        end = Offset(x = 0f, y = screenHeight)
                    )
                )
        )
        //        左下往右上渐变层
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(
                    brush = Brush.linearGradient(
                        listOf(Color(0xff149ee7), Color.Transparent),
                        start = Offset(x = 0f, y = screenHeight),
                        end = Offset(x = screenWidth, y = 0f)
                    )
                )
        )
        Column(
            verticalArrangement = Arrangement.SpaceAround,
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.fillMaxSize(),
        ) {
            Text(
                text = "用户登录",
                color = Color.White,
                fontSize = 20.sp,
                fontWeight = FontWeight.Bold
            )

            Column(
                modifier = Modifier.fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                TextField(
                    value = userViewModel.userName,
                    onValueChange = { userViewModel.userName = it },
                    singleLine = true,
                    leadingIcon = {
                        Icon(
                            imageVector = Icons.Default.Person,
                            contentDescription = null,
                            tint = Color.White
                        )
                    },
//                    label = {
//                        Text(text = "用户名", fontSize = 14.sp, color = Color.White)
//                    },
                    placeholder = {
                        Text(text = "用户名", fontSize = 14.sp, color = Color.White)
                    },
                    colors = TextFieldDefaults.colors(
                        focusedTextColor = Color.White,
                        unfocusedTextColor = Color.White,
                        focusedContainerColor = Color.Transparent,
                        unfocusedContainerColor = Color.Transparent,
                        focusedIndicatorColor = Color.LightGray,
                        unfocusedIndicatorColor = Color.LightGray,
                        focusedLabelColor = Color.LightGray,
                        unfocusedLabelColor = Color.LightGray,
                        cursorColor = Color.White,
                    ),
                    enabled = !userViewModel.loading
                )
                Spacer(modifier = Modifier.height(20.dp))
                TextField(
                    value = userViewModel.password,
                    onValueChange = { userViewModel.password = it },
                    singleLine = true,
                    leadingIcon = {
                        Icon(
                            imageVector = Icons.Default.Lock,
                            contentDescription = null,
                            tint = Color.White
                        )
                    },
                    trailingIcon = {
                        Icon(
                            imageVector = if (showPassword) Icons.Default.Favorite else Icons.Default.FavoriteBorder,
                            contentDescription = null,
                            modifier = Modifier.clickable {
                                showPassword = !showPassword
                            },
                            tint = Color.White
                        )
                    },
                    placeholder = {
                        Text(text = "密码", fontSize = 14.sp, color = Color.White)
                    },
                    visualTransformation = if (showPassword) VisualTransformation.None else PasswordVisualTransformation(),
                    colors = TextFieldDefaults.colors(
                        focusedTextColor = Color.White,
                        unfocusedTextColor = Color.White,
                        focusedContainerColor = Color.Transparent,
                        unfocusedContainerColor = Color.Transparent,
                        focusedIndicatorColor = Color.LightGray,
                        unfocusedIndicatorColor = Color.LightGray,
                        focusedLabelColor = Color.LightGray,
                        unfocusedLabelColor = Color.LightGray,
                        cursorColor = Color.White,
                    ),
                    enabled = !userViewModel.loading
                )
                Spacer(modifier = Modifier.height(28.dp))
                TextButton(onClick = {
                    coroutineScope.launch {
                        userViewModel.login(onClose = onClose)
                    }
                }, enabled = !userViewModel.loading) {
                    Row {
                        Text(text = "登录", color = Color.White, fontSize = 20.sp)
                        if (userViewModel.loading) {
                            CircularProgressIndicator(modifier = Modifier.size(20.dp))
                        }
                    }
                }
            }

            Text(text = "还没有账号？点击立即注册", color = Color.LightGray, fontSize = 14.sp)
        }
    }
}

@Preview
@Composable
fun LoginScreenPreview() {
    LoginScreen {}
}

